	deleteCR = function() {
		var m = grid.getSelections();
        if(m.length > 0) {
        	Ext.MessageBox.confirm('Mensaje', 'Esta seguro que desea eliminar el item?' , deleteCR2);	
        } else {
        	Ext.MessageBox.alert('Mensaje', 'Por favor, selecciones al menos un proyecto para eliminar.');
        }
	};
	
	deleteCR2 = function(btn) {
		if(btn == 'yes') {
			var m = grid.getSelections();
			$.ajax({
				  type: "delete",
				  url: "/clientrequirements/" + m[0].id,
				  success: function(){
								ds.remove(m[0]);
								Ext.MessageBox.alert('Exito', 'El Requerimiento de Cliente fue eliminado.');
							}
				});
		
			
		}
	};
	
	var reader = new Ext.data.XmlReader({
               record: 'requirement',
               id: 'id'
           }, [
               'name',
               'description',
			   'priority',
			   'impact',
			   'status',
			   'type'
           ]);
    // create the Data Store
	var url_cr = getProjectName() + '/clientrequirements';
	var ds = new Ext.data.GroupingStore({
				url: url_cr,
				reader: reader,
				sortInfo:{field: 'priority', direction: "ASC"},
				groupField:'priority'           
		    });

	var editorCRPrioridad = new Ext.form.ComboBox({
							store: new Ext.data.Store({ proxy: new Ext.data.HttpProxy({method: 'get', url: '/priorities'}),	reader: new Ext.data.XmlReader({ record: 'priority', id: 'id'}, ['id', 'name'])}), 
							displayField: 'name',
							valueField: 'id',
							typeAhead: true,
							mode: 'remote',
							triggerAction: 'all',
							editable: false,
							selectOnFocus: true,
							loadingText: 'Cargando...',
							width: 170
					});
	var editorCREstado = new Ext.form.ComboBox({
							store: new Ext.data.Store({ proxy: new Ext.data.HttpProxy({method: 'get', url: '/statuses'}),	reader: new Ext.data.XmlReader({ record: 'status', id: 'id'}, ['id', 'name'])}), 
							displayField: 'name',
							valueField: 'id',
							typeAhead: true,
							mode: 'remote',
							triggerAction: 'all',
							editable: false,
							selectOnFocus: true,
							loadingText: 'Cargando...',
							width: 170
					});
	var editorCRTipo = new Ext.form.ComboBox({
							store: new Ext.data.Store({ proxy: new Ext.data.HttpProxy({method: 'get', url: '/types'}),	reader: new Ext.data.XmlReader({ record: 'type', id: 'id'}, ['id', 'name'])}), 
							displayField: 'name',
							valueField: 'id',
							typeAhead: true,
							mode: 'remote',
							triggerAction: 'all',
							editable: false,
							selectOnFocus: true,
							loadingText: 'Cargando...',
							width: 170
					});
	var editorCRImpacto = new Ext.form.ComboBox({
							store: new Ext.data.Store({ proxy: new Ext.data.HttpProxy({method: 'get', url: '/impacts'}),	reader: new Ext.data.XmlReader({ record: 'impact', id: 'id'}, ['id', 'name'])}), 
							displayField: 'name',
							valueField: 'id',
							typeAhead: true,
							mode: 'remote',
							triggerAction: 'all',
							editable: false,
							selectOnFocus: true,
							loadingText: 'Cargando...',
							width: 170
					});
    var cm =new Ext.grid.ColumnModel([
			{header: "Nombre", width: 300, dataIndex: 'name', editor: new Ext.form.TextField({allowBlank: false})},
			{header: "Descripcion", width: 180, dataIndex: 'description', editor: new Ext.form.TextField({allowBlank: false})},
			{header: "Prioridad", width: 100, dataIndex: 'priority', editor: editorCRPrioridad},
			{header: "Impacto", witdh: 100, dataIndex: 'impact', editor: editorCRImpacto},
			{header: "Estado", width: 200, dataIndex: 'status', editor: editorCREstado},
			{header: "Tipo", width: 250, dataIndex: 'type', editor: editorCRTipo}
	]);
	
    cm.defaultSortable = true;

	var viewGroup = new Ext.grid.GroupingView({
			forceFit:true,
			groupTextTpl: '{text} ({[values.rs.length]} {[values.rs.length > 1 ? "Items" : "Item"]})'
		});
	
	var sm = new Ext.grid.RowSelectionModel({singleSelect:true});
    
    var grid = new Ext.grid.EditorGridPanel({
		ds: ds,
        cm: cm,
		selModel: sm,
		region: 'center',
        width:800,
        height:450,
        view: viewGroup,
		frame:true,
		collapsible: true,
		title: 'Requerimientos de Cliente',
		iconCls: 'icon-grid',
		tbar:[{
            text:'Eliminar',
            tooltip:'Eliminar requerimiento de cliente seleccionado',
            iconCls:'remove',
			handler: deleteCR
			},
			{
			text:'Asociar',
            tooltip:'Asociar Requerimiento de Cliente con Requerimiento de Sistema',
            iconCls:'associate',
			handler: agregarAssocCRPR
			}			
		]
    });
	
    ds.load();
	
	
	grid.on('afteredit', function(e) { 
		xmlDocument = '<?xml version="1.0" encoding="UTF-8" ?><requirement>';
		xmlDocument += '<' + e.field + '>' + e.value + '</' + e.field + '>';
		xmlDocument += '</requirement>';
		alert(xmlDocument);
		$.ajax({
				  type: "put",
				  url: "/clientrequirements/" + e.record.id,
				  dataType: "xml",
				  data: xmlDocument,
				  complete: function(xml){
								if(xml.status != 200) {
									Ext.MessageBox.alert('Error', 'El Requerimiento de Cliente no pudo ser editado');
								} else {
									Ext.MessageBox.alert('Exito', 'El Requerimiento de Cliente fue editado');
								}
							}
				});
		
		});
/*		
	grid.on('rowclick', function(grid, rowIndex, e){
				m = grid.getSelections();
				formPanel.setTitle('Editar Proyecto');
				formPanel.buttons[0].setText('Editar');
				window.location = '/clientrequirements.html?project=' + m[0].get('name');
				
				var p = new Ext.Panel({
			        title: 'My Panel',
			        collapsible:true,
			        region: 'center',
			        width:100,
			        html: 'Hello World'
			    });
				w = Ext.get('add-project-panel').update('');
				p.render('add-project-panel');
				alert(p.rendered);
				
			});
*/